app.controller("wardenCreateCtrl", ["$scope",'sweetAlertUtil','WardenApi',"$timeout","$state",
    function($scope,sweetAlertUtil,WardenApi,$timeout,$state) {
        $scope.area = []
        $scope.chooseIds=[]
        $scope.chooseNames=[]
        $scope.roleId = 5
        $scope.all = []
        // ---------------------start 初始化角色 -------------------------------
        initRole()
        function initRole(){
            WardenApi.getUserInfo().then(function(res){
                if(res.code == 200){
                    $scope.country = res.result.citys
                }
            },function(){})
            WardenApi.getRoles().then(function(res){
                if(res.code == 200){
                    $scope.roleId = res.result[0].id
                }
            },function(){})            
        }
        // ---------------------end 初始化角色 -------------------------------


        // ---------------------start 镇 -------------------------------
        $scope.selectCountry = function(city_id){
            WardenApi.getCity({parentId:city_id}).then(function(res){
                if(res.code == 200){
                    $scope.town = res.result.list
                    $scope.all = []
                }
            },function(){})
        }
        // ---------------------end 镇 -------------------------------

        // ---------------------start 村 -------------------------------
        $scope.selectTown = function(item,$event){
        	// $($event.target).attr('disabled','disabled');
        	if(item._checked == true){
	            WardenApi.getCity({parentId:item.city_id}).then(function(res){
	                if(res.code == 200){
                        if(res.result.list.length == 0){
                            var k = {
                                city_id:item.city_id,
                                city_name:item.city_name,
                                arr:[]
                            }
                            var o = {
                                city_name:item.city_name,
                                city_id:item.city_id
                            }
                            k.arr.push(o)
                            $scope.all.push(k)
                            // $scope.chooseIds.push(item.city_id);
                            // $scope.chooseNames.push(item.city_name)
                        }else{
                            var k = {
                                city_id:item.city_id,
                                city_name:item.city_name,
                                arr:[]
                            }
                            angular.forEach(res.result.list,function(v){
                                var o = {
                                    city_name:v.city_name,
                                    city_id:v.city_id
                                }
                                k.arr.push(o)
                            })
                            $scope.all.push(k)
                        }
	                }
	            },function(){})
        	}else{
        		angular.forEach($scope.all,function(v,k){
                    if(item.city_id == v.city_id){
                        $scope.all.splice(k, 1);
                    }
                })
        	}

        }

        $scope.selectArea = function(item) {
            if (item._checked == true) {
                if($scope.chooseIds.indexOf(item.city_id) != -1){
                    return
                }
                $scope.chooseIds.push(item.city_id);
                $scope.chooseNames.push(item.city_name)
            } else {
                if($scope.chooseIds.indexOf(item.city_id) == -1){
                    return
                }
                $scope.chooseIds.splice($scope.chooseIds.indexOf(item.city_id), 1);
                $scope.chooseNames.splice($scope.chooseNames.indexOf(item.city_name), 1);
            }
        }

        $scope.delIds = function(index){
            $scope.chooseIds.splice(index, 1);
            $scope.chooseNames.splice(index, 1);
        }        
        // ---------------------end 村 -------------------------------





        // ---------------------start 提交 -------------------------------
        $scope.submit = function(){
            if($scope.name==undefined || $scope.name==""){
                sweetAlertUtil.fail("输入名字不能为空！");
                return
            }
            // var regPwd = /(?=.*\d)(?=.*[a-zA-Z])(?=.*[^a-zA-Z0-9]).{8,30}/;

            if($scope.phone.length!=11){
                sweetAlertUtil.fail("输入手机号不正确！");
                return
            }

            if($scope.pwd.length<6 || $scope.pwd.length>16){
                sweetAlertUtil.fail("密码长度不对！请重新输入!");
                return
            }

            if($scope.pwd != $scope.Cpwd){
                sweetAlertUtil.fail("两次输入密码不相同,请重新输入！");
                return
            }
            if($scope.chooseIds.length == 0){
                sweetAlertUtil.fail("至少选择一个居委会！");
                return
            }

            var params = {
                name:$scope.name,
                phone:$scope.phone,
                roleId:5,
                password:$scope.pwd,
                cityIds:$scope.chooseIds.join(",")
            }  

            sweetAlertUtil.confirm("确认保存协管员信息?", function(isconfirm) {
                if (isconfirm) {
                    WardenApi.addUser(params).then(function(res){
                        if(res.code == 200){
                            sweetAlertUtil.success("保存成功！","确定",function(){
                                $state.go("app.warden.list")
                            })     
                        }else{
                            sweetAlertUtil.fail(res.msg);
                        }
                    },function(){})
                }

            })

        }
        // ---------------------end 提交 -------------------------------
    }
]);
